<?php

/**
 * Part of the Fuel framework.
 *
 * @package Fuel
 * @version 1.0
 * @author Fuel Development Team
 * @license MIT License
 * @copyright 2010 - 2012 Fuel Development Team
 * @link http://fuelphp.com
 */
abstract class Controller extends \Fuel\Core\Controller
{

    /**
     * Current user info
     *
     * @var array
     */
    public static $viewer = FALSE;

    /**
     * Css string
     *
     * @var string
     * @return string
     */
    public static $css = '';

    /**
     * Javascript string
     *
     * @var js
     * @return string
     */
    public static $js = '';

    /**
     * Check user is logged or not
     *
     * @return boolean
     */
    protected function checkUserLogged ()
    {
        if (\SUser::singleton()->user_id == NULL) {
            return false;
        }
        return TRUE;
    }

    protected function returnLogin ($redirect = '')
    {
        if (! \Input::is_ajax()) {
            if (empty($redirect))
                $redirect = Uri::current();
            
            \Response::redirect(Uri::login($redirect));
        }
    }

    protected function set_return_data ($code = -1, $message = '', $data = array(), $extra = array())
    {
        return array(
                '_code' => $code,
                '_message' => $message,
                '_data' => $data,
                '_extra' => $extra
        );
    }

    /**
     * The 404 action for the application.
     *
     * @access public
     * @return Response
     */
    public function action_404 ()
    {
        $var['title'] = 'Page not found';
        $var['active_mainmenu'] = 'home';
        $var['content'] = View::forge('error/404');
        
        return Response::forge(View::forge('template', $var), 404);
    }

    /**
     * Paser css to string, default path is static_url + css/ + [arg]
     *
     * @access public
     * @return string
     */
    public static function set_css ()
    {
        foreach (func_get_args() as $arg) {
            
            self::$css .= "\t";
            
            // check css tring, uri or not
            if (! preg_match('|^(\w+:)?//|', $arg) or strpos($arg, \Config::get('static_url')) === 0) {
                
                self::$css .= '<link type="text/css" rel="stylesheet" href="' . \Config::get('static_url') . 'css/' . $arg . '" />';
            } 

            else {
                
                self::$css .= '<link type="text/css" rel="stylesheet" href="' . $arg . '" />';
            }
            
            self::$css .= PHP_EOL;
        }
        
        return self::$css;
    }

    /**
     * Paser javascript to string, default path is static_url + js/ + [arg]
     *
     * @access public
     * @return string
     */
    public static function set_js ()
    {
        foreach (func_get_args() as $arg) {
            
            self::$css .= "\t";
            
            // check js tring, uri or not
            if (! preg_match('|^(\w+:)?//|', $arg) or strpos($arg, \Config::get('static_url')) === 0) {
                
                self::$js .= '<script type="text/javascript" src="' . \Config::get('static_url') . 'js/' . $arg . '"></script>';
            } 

            else {
                
                self::$js .= '<script type="text/javascript" src="' . $arg . '"></script>';
            }
            
            self::$css .= PHP_EOL;
        }
        
        return self::$js;
    }

}

